package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.Tree;
import com.xinsoft.entity.dto.SysDeptDto;
import com.xinsoft.entity.dto.SysDeptUserDto;
import com.xinsoft.entity.po.SysDept;
import com.xinsoft.mapper.SysDeptMapper;
import com.xinsoft.service.SysDeptService;
import com.xinsoft.utils.BuildTree;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author lijie
* @description 针对表【sys_dept(部门表)】的数据库操作Service实现
* @createDate 2023-05-17 09:48:12
*/
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
    implements SysDeptService{

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Override
    public Tree<SysDeptDto> getDeptTree() {
        List<Tree<SysDeptDto>> trees = new ArrayList<Tree<SysDeptDto>>();
        Map<String,Object> params = new HashMap<String,Object>();
        List<SysDeptDto> sysDepts = sysDeptMapper.getDeptList();
        for (SysDeptDto sysDept : sysDepts) {
            Tree<SysDeptDto> tree = new Tree<SysDeptDto>();
            tree.setId(sysDept.getId().toString());
            tree.setParentId(sysDept.getParentId().toString());
            tree.setText(sysDept.getDeptName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<SysDeptDto> t = BuildTree.build(trees);
        return t;
    }


    @Override
    public  List<SysDeptUserDto> getDeptUserTree() {
        List<SysDeptUserDto> list = sysDeptMapper.getDeptUserList();
        return list;
    }

    @Override
    public void insertOrUpdateBatch(List<SysDept> sysDeptList) {
        sysDeptMapper.insertOrUpdateBatch(sysDeptList);
    }

    @Override
    public List<SysDeptDto> getDeptList() {
        return sysDeptMapper.getDeptList();
    }
}




